package cn.seqdata.cxf.util;

import java.lang.annotation.ElementType;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.hibernate.validator.internal.engine.ConstraintViolationImpl;
import org.hibernate.validator.internal.engine.path.PathImpl;

public final class ViolationUtils {
	private ViolationUtils() {
	}

	public static ConstraintViolation<?> createViolation(Object bean, String propName, Object value, String message) {
		PathImpl path = PathImpl.createPathFromString(propName);
		ConstraintViolation<?> violation = ConstraintViolationImpl.forBeanValidation(null, Collections.emptyMap(),
				message, Object.class, bean, bean, value, path, null, ElementType.FIELD, null);

		return violation;
	}

	public static void addToViolations(Collection<ConstraintViolation<?>> violations, Object bean, String propName,
			Object value, String message) {
		ConstraintViolation<?> violation = createViolation(bean, propName, value, message);

		violations.add(violation);
	}

	public static ConstraintViolationException violationException(Object bean, String propName, Object value,
			String message) {
		final Set<ConstraintViolation<?>> violations = new HashSet<>();

		ViolationUtils.addToViolations(violations, bean, propName, value, message);

		return new ConstraintViolationException(violations);
	}

	public static ConstraintViolationException violationException(String propName, Object value, String message) {
		return violationException(null, propName, value, message);
	}
}
